package com.get.rbccm.webapp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/trades")
public class TradingController {
	
	@Autowired
	private TradeRepository repo;
	
	@RequestMapping(method = RequestMethod.GET)
	public List findCashTrading(){
		return repo.findAll();
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public Trade addCashTrading(@RequestBody Trade cashTrading){
		cashTrading.setId(null);
		return repo.saveAndFlush(cashTrading);
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public Trade updateCashTrading(@RequestBody Trade updatedTrade, @PathVariable Integer id){
		updatedTrade.setId(id);
		return repo.saveAndFlush(updatedTrade);
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public void deleteCashTrading(@PathVariable Integer id){
		repo.delete(id);
		
	}
}
